import { createRouter, createWebHistory } from 'vue-router'
//导入进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'show',
      component: ()=>import('../components/Show.vue')
    },
    {
      path: '/login',
      name: 'login',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../components/Login.vue')
    },
    {
      path: '/home',
      name: 'home',
      component: ()=>import('../components/Home.vue'),
      children:[
        {
          path: '/roleList',
          name: 'RoleList',
          component: ()=>import('../components/RoleList.vue')
        },
        {
          path: '/authorityList',
          name: 'AuthorityList',
          component: ()=>import('../components/AuthorityList.vue')
        },
        {
          path: '/userList',
          name: 'UserList',
          component: ()=>import('../components/UserList.vue')
        },
        {
          path: '/musicList',
          name: 'MusicList',
          component: ()=>import('../components/MusicList.vue')
        },
        {
          path: '/addMusic',
          name: 'AddMusic',
          component: ()=>import('../components/AddMusic.vue')
        },
        {
          path: '/editMusic',
          name: 'EditMusic',
          component: ()=>import('../components/EditMusic.vue')
        }
      ]
    }
  ]
})

// 挂载路由守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从那个路径跳转而来
  // next 是一个函数,表示放行
  // eslint-disable-next-line no-empty

  if (to.path === '/login' || to.path === '/'){
    next();
  }

  // 获取token
  const token = window.sessionStorage.getItem('token')
  if (!token && to.path !== '/login') {
    return next('/login');
  }
  NProgress.start();
  next();
})

router.afterEach(()=>{
  NProgress.done();
})

export default router
